//
//  DateExtension.swift
//  xz-app
//
//  Created by chen on 2025/5/8.
//
import SwiftUI

extension Date {
    
    var yearMonthString: String {
        return DateFormatter.yearMonth.string(from: self)
    }
    
    var fullDateString: String {
        return DateFormatter.fullDate.string(from: self)
    }
    
    var fullDateTimeString: String {
        return DateFormatter.fullDateTime.string(from: self)
    }
    
    var monthDayString: String {
        return DateFormatter.monthDay.string(from: self)
    }
    
    var isToday: Bool {
        let calendar = Calendar.current
        return calendar.isDateInToday(self)
    }
    
    var isThisMonth: Bool {
        let calendar = Calendar.current
        let now = Date()
        return calendar.isDate(self, equalTo: now, toGranularity: .month)
    }
}

extension DateFormatter {
    
    static let yearMonth: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM"
        return formatter
    }()
    
    static let fullDate: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        return formatter
    }()
    
    static let fullDateTime: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return formatter
    }()
    
    static let monthDay: DateFormatter = {
        let formatter = DateFormatter()
        formatter.dateFormat = "MM-dd"
        return formatter
    }()
}
